home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / cdbw.zip / SAMPLE.C < prev    next >
Text File  |  1991-05-15  |  10KB  |  338 lines

  1. /*
  2.  *  SAMPLE.C
  3.  *
  4.  *  This module contains the WinMain, WndProc, and initialization functions
  5.  *  for SAMPLE.EXE
  6.  *
  7.  *  Copyright (C) 1991 by Daytris.  All rights reserved.
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include "dbmgr.h"
  13. #include "sampledb.h"
  14. #include "sample.h"
  15.  
  16.  
  17. /************************************************
  18.  * Function Declarations
  19.  ************************************************/
  20.  
  21. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
  22.     int nCmdShow);
  23. BOOL InitApplication( HANDLE hInstance);
  24. HWND InitInstance( HANDLE hInstance, short nCmdShow);
  25. static BOOL InitDatabase( HWND hParentWnd);
  26. long FAR PASCAL WndProc( HWND hWnd, unsigned uMessage, WORD wParam,
  27.     LONG lParam);
  28. static void InitMainWindow( HWND hWnd);
  29.  
  30.  
  31. /************************************************
  32.  * Local Data
  33.  ************************************************/
  34.  
  35. static char szAppName[] = "Sample";         /* Class name */
  36.  
  37.  
  38. /************************************************
  39.  * Global Data
  40.  ************************************************/
  41.  
  42. HANDLE hInst;                               /* Instance handle */
  43. HANDLE hDb;                                 /* Database handle */
  44. HWND hWndClientLB;                          /* Client listbox handle */
  45. SETUP setup;                                /* Setup record */
  46. BOOL bSortByNumber = TRUE;                  /* Client listbox sort seq */
  47.  
  48.  
  49. /***************************************************************************
  50.  * Function : WinMain
  51.  *
  52.  * Purpose  : This function is the entry function for SAMPLE.EXE.
  53.  *
  54.  * Returns  : FALSE - error in initialization
  55.  *            MSG.wParam
  56.  ***************************************************************************/
  57. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
  58.     int nCmdShow)
  59.     {
  60.     HANDLE hWnd;
  61.     MSG msg;
  62.  
  63.     if( ! hPrevInstance)
  64.         {
  65.         if( ! InitApplication( hInstance))
  66.             return FALSE;
  67.         }
  68.  
  69.     if( ! (hWnd = InitInstance( hInstance, nCmdShow)) )
  70.         return FALSE;
  71.  
  72.     /* Initialize the database */
  73.     if( ! InitDatabase( hWnd))
  74.         return FALSE;
  75.  
  76.     /* Load the client listbox */
  77.     LoadClientListBox( hWnd);
  78.  
  79.     while( GetMessage( &msg, NULL, NULL, NULL))
  80.         {
  81.         TranslateMessage( &msg);
  82.         DispatchMessage( &msg);
  83.         }
  84.  
  85.     return msg.wParam;
  86.     }
  87.  
  88.  
  89. /***************************************************************************
  90.  * Function : InitApplication
  91.  *
  92.  * Purpose  : This function registers the SAMPLE class.
  93.  *
  94.  * Returns  : RegisterClass return value
  95.  ***************************************************************************/
  96. BOOL InitApplication( HANDLE hInstance)
  97.     {
  98.     WNDCLASS wc;
  99.  
  100.     wc.style            = NULL;
  101.     wc.lpfnWndProc      = WndProc;
  102.     wc.cbClsExtra       = 0;
  103.     wc.cbWndExtra       = DLGWINDOWEXTRA;
  104.     wc.hInstance        = hInstance;
  105.     wc.hIcon            = LoadIcon( NULL, IDI_APPLICATION);
  106.     wc.hCursor          = LoadCursor( NULL, IDC_ARROW);
  107.     wc.hbrBackground    = GetStockObject( WHITE_BRUSH);
  108.     wc.lpszMenuName     = (LPSTR)"SAMPLE";
  109.     wc.lpszClassName    = (LPSTR)szAppName;
  110.  
  111.     return( RegisterClass( &wc));
  112.     }
  113.  
  114.  
  115. /***************************************************************************
  116.  * Function : InitInstance
  117.  *
  118.  * Purpose  : The function initializes the instance creating a main
  119.  *            window.
  120.  *
  121.  * Returns  : >0   - window handle
  122.  *            NULL - window not created
  123.  ***************************************************************************/
  124. HWND InitInstance( HANDLE hInstance, short nCmdShow)
  125.     {
  126.     HWND hWnd;
  127.  
  128.     /* Save the instance handle globally */
  129.     hInst = hInstance;
  130.  
  131.     /* Create the main window */
  132.     hWnd = CreateWindow( szAppName,
  133.         "CDB Sample Program (Client List)",
  134.         WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU |
  135.             WS_MINIMIZEBOX | WS_THICKFRAME,
  136.         CW_USEDEFAULT,
  137.         CW_USEDEFAULT,
  138.         CW_USEDEFAULT,
  139.         CW_USEDEFAULT,
  140.         NULL,
  141.         NULL,
  142.         hInstance,
  143.         NULL);
  144.     if( ! hWnd)
  145.         return NULL;
  146.  
  147.     /* Display the window */
  148.     ShowWindow( hWnd, nCmdShow);
  149.     UpdateWindow( hWnd);
  150.  
  151.     return hWnd;
  152.     }
  153.  
  154.  
  155. /***************************************************************************
  156.  * Function : InitDatabase
  157.  *
  158.  * Purpose  : This function opens SAMPLEDB.DBD and retrieves the setup
  159.  *            record.  If no setup record exists, one is created.  Only
  160.  *            one setup record is used in SAMPLE.EXE.
  161.  *
  162.  * Returns  : TRUE  - database initialized
  163.  *            FALSE - database not initialized
  164.  ***************************************************************************/
  165. static BOOL InitDatabase( HWND hParentWnd)
  166.     {
  167.     DWORD dwStatus;
  168.  
  169.     /* Open the sample database.  Use the current directory for database
  170.        file creation and retrieval */
  171.     if( dwStatus = DbOpen( hParentWnd, ".\\", "sampledb.dbd", &hDb))
  172.         {
  173.         DbError( hParentWnd, dwStatus, __FILE__, __LINE__);
  174.         return FALSE;
  175.         }
  176.  
  177.     /* Get the setup record.  If it does not exist, create one. */
  178.     dwStatus = XDbRecordGetFirst( hDb, "setup", "lNextClientNbr", &setup,
  179.         sizeof( SETUP));
  180.     if( dwStatus == E_NOTFOUND)
  181.         {
  182.         /* Starting client number is 1000.  Add record. */
  183.         setup.lNextClientNbr = 1000L;
  184.         dwStatus = XDbRecordAdd( hDb, "setup", &setup, sizeof( SETUP));
  185.         }
  186.     if( dwStatus)
  187.         {
  188.         DbError( hParentWnd, dwStatus, __FILE__, __LINE__);
  189.         DbClose( hDb);
  190.         return FALSE;
  191.         }
  192.  
  193.     /* Flush the setup record to disk */
  194.     DbFlush( hDb);
  195.  
  196.     return TRUE;
  197.     }
  198.  
  199.  
  200. /***************************************************************************
  201.  * Function : WndProc
  202.  *
  203.  * Purpose  : This function is the window procedure for SAMPLE.EXE
  204.  *
  205.  * Returns  : 0L - message processed by function
  206.  *            DefWndProc value
  207.  ***************************************************************************/
  208. long FAR PASCAL WndProc( HWND hWnd, unsigned uMessage, WORD wParam,
  209.     LONG lParam)
  210.     {
  211.     HANDLE hMenu;
  212.  
  213.     switch( uMessage)
  214.         {
  215.         case WM_CREATE:
  216.             InitMainWindow( hWnd);
  217.             return 0L;
  218.  
  219.         case WM_SETFOCUS:
  220.             SetFocus( hWndClientLB);
  221.             return 0L;
  222.  
  223.         case WM_COMMAND:
  224.             switch( wParam)
  225.                 {
  226.                 case IDC_CLIENT_LISTBOX:
  227.                     if( HIWORD( lParam) == LBN_DBLCLK)
  228.                         SendMessage( hWnd, WM_COMMAND, IDM_UPDATE, 0L);
  229.                     break;
  230.  
  231.                 case IDM_ADD:
  232.                     AddClientDlg( hWnd);
  233.                     break;
  234.  
  235.                 case IDM_UPDATE:
  236.                     UpdateClientDlg( hWnd);
  237.                     break;
  238.  
  239.                 case IDM_DELETE:
  240.                     DeleteClientDlg( hWnd);
  241.                     break;
  242.  
  243.                 case IDM_SORT_BY_NUMBER:
  244.                     if( bSortByNumber)
  245.                         break;
  246.                     hMenu = GetMenu( hWnd);
  247.                     CheckMenuItem( hMenu, IDM_SORT_BY_NAME, MF_UNCHECKED);
  248.                     CheckMenuItem( hMenu, IDM_SORT_BY_NUMBER, MF_CHECKED);
  249.                     bSortByNumber = TRUE;
  250.                     LoadClientListBox( hWnd);
  251.                     break;
  252.  
  253.                 case IDM_SORT_BY_NAME:
  254.                     if( ! bSortByNumber)
  255.                         break;
  256.                     hMenu = GetMenu( hWnd);
  257.                     CheckMenuItem( hMenu, IDM_SORT_BY_NUMBER, MF_UNCHECKED);
  258.                     CheckMenuItem( hMenu, IDM_SORT_BY_NAME, MF_CHECKED);
  259.                     bSortByNumber = FALSE;
  260.                     LoadClientListBox( hWnd);
  261.                     break;
  262.  
  263.                 case IDM_ABOUT:
  264.                     AboutDlg( hWnd);
  265.                     break;
  266.  
  267.                 case IDM_EXIT:
  268.                     PostMessage( hWnd, WM_DESTROY, 0, 0L);
  269.                     break;
  270.                 }
  271.             return 0L;
  272.  
  273.         case WM_DESTROY:
  274.             {
  275.             DWORD dwStatus;
  276.  
  277.             if( dwStatus = DbClose( hDb))
  278.                 DbError( hWnd, dwStatus, __FILE__, __LINE__);
  279.             PostQuitMessage( 0);
  280.             return 0L;
  281.             }
  282.         }
  283.  
  284.     return( DefWindowProc( hWnd, uMessage, wParam, lParam));
  285.     }
  286.  
  287.  
  288. /***************************************************************************
  289.  * Function : InitMainWindow
  290.  *
  291.  * Purpose  : This function sizes the main window and creates a client
  292.  *            listbox window.
  293.  *
  294.  * Returns  : n/a
  295.  ***************************************************************************/
  296. static void InitMainWindow( HWND hWnd)
  297.     {
  298.     short xChar, yChar;
  299.     HDC hDC;
  300.     TEXTMETRIC tm;
  301.     RECT rect;
  302.  
  303.     /* Get the default window parameters */
  304.     hDC = GetDC( hWnd);
  305.     GetTextMetrics( hDC, &tm);
  306.     ReleaseDC( hWnd, hDC);
  307.     GetWindowRect( hWnd, &rect);
  308.     xChar = tm.tmAveCharWidth;
  309.     yChar = tm.tmHeight + tm.tmExternalLeading;
  310.  
  311.     /* Size the window */
  312.     MoveWindow( hWnd,
  313.         rect.left,
  314.         rect.top,
  315.         xChar * WINDOW_WIDTH + (GetSystemMetrics( SM_CXFRAME) * 2),
  316.         (yChar * (LISTBOX_Y + 1)) + (tm.tmHeight * LISTBOX_LENGTH) +
  317.             GetSystemMetrics( SM_CYCAPTION) + GetSystemMetrics( SM_CYMENU),
  318.         FALSE);
  319.  
  320.     /* Create the listbox */
  321.     GetClientRect( hWnd, &rect);
  322.     hWndClientLB = CreateWindow( "listbox",
  323.         NULL,
  324.         WS_CHILD | WS_VISIBLE | LBS_STANDARD,
  325.         rect.left + (xChar * LISTBOX_X),
  326.         rect.top + (yChar * LISTBOX_Y),
  327.         xChar * LISTBOX_WIDTH,
  328.         tm.tmHeight * LISTBOX_LENGTH,
  329.         hWnd,
  330.         IDC_CLIENT_LISTBOX,
  331.         hInst,
  332.         NULL);
  333.  
  334.     /* Set listbox font to fixed size */
  335.     SendMessage( hWndClientLB, WM_SETFONT, GetStockObject( SYSTEM_FIXED_FONT),
  336.         0L);
  337.     }
  338.